package com.woniuxy.yoga.business.mq;



import com.woniuxy.yoga.business.client.AdminClient;
import com.woniuxy.yoga.business.dao.mapper.BusinessPaymentMapper;
import com.woniuxy.yoga.business.dao.model.BusinessPayment;
import com.woniuxy.yoga.business.model.exception.BusinessException;
import com.woniuxy.yoga.business.model.exception.BusinessExceptionCode;
import com.woniuxy.yoga.business.model.form.businesspayment.PaymentLogForm;
import com.woniuxy.yoga.commons.core.model.Result;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class CreatePaymentLogConsumer {

    @Autowired
    private BusinessPaymentMapper businessPaymentMapper;
    @Autowired
    private AdminClient adminClient;
    /**
     * 生成商家缴纳入驻费日志
     * @param paymentLogForm
     * @throws Exception
     */
    @RabbitListener(queuesToDeclare = {@Queue(name = "create-businessPaymentLog")})
    @RabbitHandler
    public void consumer(PaymentLogForm paymentLogForm) throws Exception{
        BusinessPayment businessPayment = businessPaymentMapper.selectById(paymentLogForm.getPaymentId());
        if (businessPayment==null) {
            throw new BusinessException(BusinessExceptionCode.BUSINESS_EXCEPTION_CODE_PAYMENT_NOTEXIST);
        }
        Result<Boolean> result = adminClient.addPaymentLog(paymentLogForm);
        if (result.getCode()==500) {
            throw new BusinessException(500,"系统错误");
        }
        if (result.getCode()!=200) {
            throw new BusinessException(result.getCode(),result.getMessage());
        }
    }

}
